package com.example.sports_meet.athlete;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;
import com.example.sports_meet.adapter.SelectAdapter;
import com.example.sports_meet.entity.SelectM;

import java.util.ArrayList;
import java.util.List;

public class Athlete_deleteMessage extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private Button btnSelect, btnDelete;
    private EditText editKey;
    private ListView listView;
    private List<SelectM> books = new ArrayList<SelectM>();
    MyDbHelper myDbHelper;
    SQLiteDatabase db;
    SelectM selectMss;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_athlete_delete_message);
        myDbHelper = new MyDbHelper(Athlete_deleteMessage.this, "Sports.db", null, 1);
        btnSelect = (Button) findViewById(R.id.btnselect);
        btnDelete = (Button) findViewById(R.id.btnsc);
        editKey = (EditText) findViewById(R.id.editkey);
        listView = (ListView) findViewById(R.id.listview);

        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editKey.getText().toString();
                books.clear();
                SelectM selectMs = new SelectM("编号", "姓名", "性别");
                books.add(selectMs);

                db = myDbHelper.getReadableDatabase();
                Cursor cursor = db.query("athlete", new String[]{"ano", "aname", "asex"}, "aname=?", new String[]{name}, null, null, null);
                if (cursor.getCount() == 0) {
                    Toast.makeText(Athlete_deleteMessage.this, "没有数据", Toast.LENGTH_LONG).show();
                } else {
                    cursor.moveToFirst();
                    for (int i = 0; i < cursor.getCount(); i++) {
                        String ano = cursor.getString(0);
                        String aname = cursor.getString(1);
                        String asex = cursor.getString(2);
                        SelectM selectM = new SelectM(ano, aname, asex);
                        books.add(selectM);
                    }
                }
                cursor.close();
                db.close();
                SelectAdapter selectAdapter = new SelectAdapter(books, Athlete_deleteMessage.this);
                listView.setAdapter(selectAdapter);
            }
        });
        listView.setOnItemClickListener(this);
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //db=managerAthleteHelper.getWritableDatabase();
                db = myDbHelper.getWritableDatabase();
                db.delete("athlete", "ano = ?", new String[]{selectMss.getNo()});
                Toast.makeText(Athlete_deleteMessage.this, "删除成功", Toast.LENGTH_LONG).show();
                db.close();
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        selectMss = books.get(i);
    }
}